﻿using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Messaging;
using Microsoft.Research.DynamicDataDisplay.Common.Auxiliary;
using SuperMicroWave.Common;
using SuperMicroWave.LogFile;
using SuperMicroWave.Messages;
using System;
using System.Threading;
using System.Threading.Tasks;
using System.Timers;
using System.Windows;
using System.Windows.Threading;

namespace SuperMicroWave.ViewModels
{
    public partial class InflatingViewModel : ObservableObject
    {
        [ObservableProperty]
        public string buttonContent;

        [ObservableProperty]
        public bool isBtnEnable;

        [ObservableProperty]
        public bool isWaited;

        [ObservableProperty]
        public string info;

        public string[] InfoList = new string[3] { "预加压中，请稍等.", "预加压中，请稍等..", "预加压中，请稍等..." };
        public int index;


        public InflatingViewModel()
        {
            ButtonContent = "确定";
            Info = "预加压中，请稍等.";
            index = 0;
            IsBtnEnable = false;
            IsWaited = true;

            Thread thread1 = new Thread(new ThreadStart(UpdateState));
            thread1.IsBackground = true;
            thread1.Start();
        }

        public async void UpdateState()
        {
            GlobalValue.modBusTcp.WriteCoil("3", true);  //打开冷水机
            GlobalValue.modBusTcp.WriteCoil("2", true);  //打开冷水阀
            GlobalValue.modBusTcp.WriteCoil("4", true);   //打开排气扇
            GlobalValue.modBusTcp.WriteCoil("1", false);  //排气阀
            await Task.Delay(5000);
            GlobalValue.modBusTcp.WriteCoil("0", true);  //进气阀

            double srcP = GlobalValue.methodData.PrePressure;
            double targetP = Math.Round(srcP * GlobalValue.sysParams.PreScale, 2);
            bool flag = true;  //是否补压
            while (true)
            {
                if (GlobalValue.NowState.P1 < targetP)
                {
                    Info = InfoList[(index++) % 3];
                }
                else
                {
                    GlobalValue.modBusTcp.WriteCoil("0", false);  //进气阀
                    Info = "加压完成，请等待压力值稳定...";

                    int time = GlobalValue.sysParams.StabTime;
                    while(true)
                    {
                        if(GlobalValue.NowState.P1 < srcP * GlobalValue.sysParams.StabScale)
                        {
                            if(flag)
                            {
                                flag = false;
                                LogHelper.WriteInfoLog($"第一次：{GlobalValue.NowState.P1} < {srcP * GlobalValue.sysParams.StabScale}，等待15s进行补压");
                                await Task.Delay(15 * 1000);   //等待压力稳定再充压
                                LogHelper.WriteInfoLog("打开进气阀补压");
                                GlobalValue.modBusTcp.WriteCoil("0", true);  //进气阀
                                break;
                            }
                            else
                            {
                                LogHelper.WriteInfoLog($"{GlobalValue.NowState.P1} < {srcP * GlobalValue.sysParams.StabScale}，压力值下降过快");
                                Info = "压力值下降过快，请检查是否存在漏气";
                                ButtonContent = "继续";
                                IsBtnEnable = true;
                                IsWaited = false;
                                return;
                            }
                        }
                        else if(time == 0)
                        {
                            LogHelper.WriteInfoLog("稳压时间到，关闭加压窗口开始升温");
                            WeakReferenceMessenger.Default.Send(new CloseWindowMessage());
                            return;
                        }

                        await Task.Delay(1000);
                        time--;
                    }

                    /*
                    if (GlobalValue.NowState.P1 >= srcP * GlobalValue.sysParams.StabScale)
                    {
                        //Info = "压力值已趋于稳定，点击确定按钮即将开始升温";
                        //IsBtnEnable = true;
                        //IsWaited = false;
                        CloseWindow?.Invoke();
                        return;
                    }
                    else if (flag)
                    {
                        flag = false;
                        GlobalValue.modBusTcp.WriteCoil("0", true);  //进气阀
                        continue;
                    }
                    else
                    {
                        Info = "压力值下降过快，请检查是否存在漏气";
                        IsWaited = false;
                        return;
                    }*/
                }

                Thread.Sleep(1000);
            }            
        }
    }
}
